/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.inventory.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtAccessoryInout;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtAccessoryStore;
import com.thinkgem.jeesite.modules.inventory.entity.ZlmtHostsAccessory;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtAccessoryInoutService;
import com.thinkgem.jeesite.modules.inventory.service.ZlmtAccessoryStoreService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 备件Controller
 * @author johnKee
 * @version 2016-07-28
 */
@Controller
@RequestMapping(value = "${adminPath}/inventory/zlmtAccessoryInout")
public class ZlmtAccessoryInoutController extends BaseController {

	@Autowired
	private ZlmtAccessoryInoutService zlmtAccessoryInoutService;

    @Autowired
    private ZlmtAccessoryStoreService zlmtAccessoryStoreService;
	
	@ModelAttribute
	public ZlmtAccessoryInout get(@RequestParam(required=false) String id) {
		ZlmtAccessoryInout entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = zlmtAccessoryInoutService.get(id);
		}
		if (entity == null){
			entity = new ZlmtAccessoryInout();
		}
		return entity;
	}
	

	@RequestMapping(value = {"list", ""})
	public String list(ZlmtAccessoryInout zlmtAccessoryInout, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ZlmtAccessoryInout> page = zlmtAccessoryInoutService.findPage(new Page<ZlmtAccessoryInout>(request, response), zlmtAccessoryInout); 
		model.addAttribute("page", page);
		return "modules/inventory/zlmtAccessoryInoutList";
	}


	@RequestMapping(value = "form")
	public String form(ZlmtAccessoryInout zlmtAccessoryInout, Model model) {
		model.addAttribute("zlmtAccessoryInout", zlmtAccessoryInout);
		return "modules/inventory/zlmtAccessoryInoutForm";
	}

	@RequestMapping(value = "inout")
	public String inout(ZlmtAccessoryInout zlmtAccessoryInout, Model model){
        if(StringUtils.isNotEmpty(zlmtAccessoryInout.getAccessoryStoreId())){
            String accessoryName = zlmtAccessoryStoreService.get(zlmtAccessoryInout.getAccessoryStoreId()).getAccessoryName();
            model.addAttribute("accessoryName", accessoryName);
        }
		model.addAttribute("zlmtAccessoryInout", zlmtAccessoryInout);
		return "modules/inventory/zlmtAccessoryInoutDetail";
	}


	@RequestMapping(value = "save")
	public String save(ZlmtAccessoryInout zlmtAccessoryInout, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, zlmtAccessoryInout)){
			return form(zlmtAccessoryInout, model);
		}
        boolean shouldGroupSave = false;
        ZlmtAccessoryStore zlmtAccessoryStore = null;
		ZlmtHostsAccessory hostsAccessory = null;
        if(StringUtils.isNotEmpty(zlmtAccessoryInout.getAccessoryStoreId())){
            zlmtAccessoryStore = zlmtAccessoryStoreService.get(zlmtAccessoryInout.getAccessoryStoreId());
            int current = Integer.parseInt(zlmtAccessoryStore.getAmount());
            int change = Integer.parseInt(zlmtAccessoryInout.getAmount());
            if(zlmtAccessoryInout.getInoutType().equals("OUT")){
                if(current < change){
                    model.addAttribute("zlmtAccessoryInout", zlmtAccessoryInout);
                    model.addAttribute("accessoryName", zlmtAccessoryStore.getAccessoryName());
                    model.addAttribute("message","出库数量不能大于库存！");
                    return "modules/inventory/zlmtAccessoryInoutDetail";
                }else {
                    current = current -change;
                    zlmtAccessoryStore.setAmount(String.valueOf(current));
                    shouldGroupSave = true;

					hostsAccessory = new ZlmtHostsAccessory();
					hostsAccessory.setAmount(String.valueOf(change));
					hostsAccessory.setAccessoryName(zlmtAccessoryStore.getAccessoryName());
					hostsAccessory.setContact(UserUtils.getUser().getName()+" "+UserUtils.getUser().getMobile());
					hostsAccessory.setAccessoryCategory(zlmtAccessoryStore.getAccessoryCategory());
					hostsAccessory.setHostid(zlmtAccessoryInout.getHostid());
					hostsAccessory.setBrand(zlmtAccessoryStore.getBrand());
					hostsAccessory.setModel(zlmtAccessoryStore.getModel());
					hostsAccessory.setInuseDate(new Date());
					hostsAccessory.setWarrantyDate(zlmtAccessoryStore.getWarrantyDate());
                }
            } else if(zlmtAccessoryInout.getInoutType().equals("IN")){
                current = current + change;
                zlmtAccessoryStore.setAmount(String.valueOf(current));
                shouldGroupSave =true;
            }
        }
        if(shouldGroupSave){
            zlmtAccessoryStoreService.groupSave(zlmtAccessoryStore, zlmtAccessoryInout, hostsAccessory);
        }else {
            zlmtAccessoryInoutService.save(zlmtAccessoryInout);
        }
		addMessage(redirectAttributes, "保存备件出入库成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/zlmtAccessoryStore/?repage";
	}
	

	@RequestMapping(value = "delete")
	public String delete(ZlmtAccessoryInout zlmtAccessoryInout, RedirectAttributes redirectAttributes) {
		zlmtAccessoryInoutService.delete(zlmtAccessoryInout);
		addMessage(redirectAttributes, "删除主机备件成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/zlmtAccessoryInout/?repage";
	}

}